package com.zxl.live.screen.call.ui.widget;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.TextureView;

import com.volokh.danylo.video_player_manager.ui.ScalableTextureView;
import com.zxl.live.tools.tools.LogUtils;

/**
 * Created by Administrator on 2017/12/10.
 */

public class VideoPlayerView extends ScalableTextureView implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, TextureView.SurfaceTextureListener, MediaPlayer.OnErrorListener {

    private boolean mIsStop;

    private MediaPlayer mMediaPlayer;

    public VideoPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setScaleType(ScalableTextureView.ScaleType.CENTER_CROP);
        setSurfaceTextureListener(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stop();
    }

    public void play(String path) {
        try {
            mIsStop = false;
            mMediaPlayer = new MediaPlayer();
            SurfaceTexture surfaceTexture = getSurfaceTexture();
            if (null != surfaceTexture) {
                mMediaPlayer.setSurface(new Surface(surfaceTexture));
            }
            mMediaPlayer.setOnErrorListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        try {
            mIsStop = true;
            if (null != mMediaPlayer && mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        if (!mIsStop) {
            LogUtils.d("onCompletion" + mp.getCurrentPosition());
            mp.seekTo(0);
            mp.start();
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        if (!mIsStop) {
            mp.start();
        }
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        LogUtils.d("onError : " + extra);
        mIsStop = true;
        return true;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        if (null != mMediaPlayer) {
            mMediaPlayer.setSurface(new Surface(surface));
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }


}
